package com.stephenone.zhdaily.comment;

import android.util.Log;

import com.google.common.base.Optional;
import com.stephenone.zhdaily.data.bean.Comments;
import com.stephenone.zhdaily.data.source.CommentsRepository;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/1/30.
 */

public class CommentPresenter implements CommentContract.Presenter {
    private static final String TAG = CommentPresenter.class.getSimpleName();
    private CommentContract.View mView;
    private CompositeDisposable mCompositeDisposable;
    private CommentsRepository mCommentsRepository;
    private long mStoryId;
    public CommentPresenter(CommentsRepository commentsRepository,CommentContract.View view, long storyId){
        mCommentsRepository = commentsRepository;
        mView = view;
        mStoryId = storyId;
        mCompositeDisposable = new CompositeDisposable();
        mView.setPresenter(this);
    }

    @Override
    public void subscribe() {
        getLongComment();
    }

    private void getLongComment() {
        mCompositeDisposable.add(mCommentsRepository.getLongComments(mStoryId)
                .filter(Optional::isPresent)
                .map(Optional::get)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(comments -> {
                    Log.e(TAG, "getLongComment: " + "666" );
                    showLongComment(comments);
                }));
    }

    private void showLongComment(Comments comments) {
        mView.showLongComment(comments);
    }

    @Override
    public void unsubscribe() {

    }

    @Override
    public void initShortComment() {
        mCompositeDisposable.add(mCommentsRepository.getShortComments(mStoryId)
                .filter(Optional::isPresent)
                .map(Optional::get)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(comments -> {
                    Log.e(TAG, "getShortComment: " + "666" );
                    showShortComment(comments);
                }));
    }

    @Override
    public void loadMore(long finalCommentId) {
        mCompositeDisposable.add(mCommentsRepository.getBeforeComments(mStoryId,finalCommentId)
                .filter(Optional::isPresent)
                .map(Optional::get)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(comments -> {
                    Log.e(TAG, "getMore: " + "666" );
                    showMoreComment(comments);
                }));
    }

    private void showShortComment(Comments comments) {
        mView.showShortComment(comments);
    }

    private void showMoreComment(Comments comments) {
        mView.showMoreComment(comments);
    }

}
